#    One Room And A Goblin Invasion
#    Copyright (C) 2014  Jannik Haberbosch
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


# Python modules
import math

# 3rd party modules
import pygame
pygame.init()

# Project modules
from code import helpers


class Projectile( pygame.sprite.Sprite ):
	"""A projectile is a little bullet that can be fired by the player using the
	left mouse-button.

	"""

	# Class variables.
	# Each bullet uses the same image, thus we an use a class variable.
	image = None

	# Each bullet has the same speed and thus we can use a class variable.
	speed = None


	test = []

	
	def __init__( self,
		direction, # in degrees
		position_center
		):
		"""
		Parameters
			direction
			integer
			Defines where the fired projectile should head towards to.  It has
			to be a value between 1-359 (degrees).
			
			position_center
			tuple, list
			Either a tuple or a list that consists of two integer values that
			define the initial position of the projectile before it moves.

		"""
		# First of all, we need to call the parents' constructor.
		pygame.sprite.Sprite.__init__( self )

		self.direction = direction
		self.position_center = position_center
		
		self.image = pygame.transform.rotate(
			Projectile.image.copy(),
			self.direction
		)
		self.rect = self.image.get_rect()
		self.rect.center = self.position_center

		# Calculation of the speed per frame for both the x -and y axis.
		self.x = math.cos( math.radians( self.direction ) ) * Projectile.speed
		self.y = -math.sin( math.radians( self.direction ) ) * Projectile.speed

		# I use seperate float variables in order to be able to fire in every
		# single degree.
		self.x_float = self.rect.centerx
		self.y_float = self.rect.centery


	def update( self, visible_area, furniture, window ):
		"""Updates a Projectile object.

		Parameters
			visible_area
			pygame.Rect
			Represents the window.

			furniture
			list
			A list that consists of many pygame.Rect objects that are used to
			detect whether a fired projectile has hit a furniture of the room.

			window
			pygame.Surface
			An object that represents the window of the game.  Not actually
			needed anymore.

		"""

		# The player can not fire through the furniture.
		for f in furniture:
			if f.colliderect( self.rect ):
				self.kill()
				Projectile.test.append( self )

		# If the projectile has not left the window then its' position is
		# updated.
		if not visible_area.contains( self.rect ):
			self.kill()
		else:
			self.x_float += self.x
			self.y_float += self.y
			self.rect.center = ( self.x_float, self.y_float )


	@staticmethod
	def load_image( imagefile ):
		"""A static method that handles loading a file.

		I might remove this method and set the image directly.

		"""
		Projectile.image = helpers.load_image(
			imagefile
		)


	@staticmethod
	def set_speed( speed ):
		"""A static method to set the speed.

		I might remove this method and set the image directly.

		"""
		Projectile.speed = speed
